﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PaperFighter;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;

namespace PaperFighter
{
    class Welcome : GameScreen
    {
        Texture2D background;
        Texture2D logo;
        Texture2D start;
        Vector2 logoPosition;
        Vector2 startPosition;
        Vector2 planePosition;
        Vector2 planeDirection;
        SoundEffect clickSound;
        List<Texture2D> plane = new List<Texture2D>(3);
        Random rand = new Random();

        int planeIndex = 0;
        int frameCount = 300;       
        bool startClick = false;
        float planeSize = 1.0f;
        float exitSpeed = 1;
        
        public Welcome()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.0);
            TransitionOffTime = TimeSpan.FromSeconds(0.0);
        }

        public override void LoadContent()
        {
            background = ScreenManager.Game.Content.Load<Texture2D>("background_1");
            logo = ScreenManager.Game.Content.Load<Texture2D>("logo_1");
            start = ScreenManager.Game.Content.Load<Texture2D>("start_1");
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_1_1"));
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_1_2"));
            plane.Add(ScreenManager.Game.Content.Load<Texture2D>("plane_1_3"));

            clickSound = ScreenManager.Game.Content.Load<SoundEffect>("click_1");

            logoPosition = new Vector2(240 - logo.Width / 2, 150);
            startPosition = new Vector2(240 - start.Width / 2, 500);
            
            base.LoadContent();
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            PlayerIndex playerIndex;
            if (input.IsMenuCancel(null, out playerIndex))
                ScreenManager.Game.Exit();

            if (input.TouchState.Count > 0)
            {
                int touchPositionY = (int)input.TouchState[0].Position.Y;
                if (touchPositionY > 500 && touchPositionY < 605)
                    startClick = startClick == false ? clickSound.Play() : true;
            }
            
            base.HandleInput(input);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            planePosition += planeDirection;
            frameCount++;

            if (frameCount > 150)
            {
                planeIndex = ++planeIndex % 3;
                switch (planeIndex)
                {
                    case 0:
                        planePosition = new Vector2(rand.Next(200, 480), -138);
                        planeDirection = new Vector2(-6, 10);
                        break;
                    case 1:
                        planePosition = new Vector2(rand.Next(-127, 100), 800);
                        planeDirection = new Vector2(4, -10);
                        break;
                    case 2:
                        planePosition = new Vector2(-170, rand.Next(0, 400));
                        planeDirection = new Vector2(6, 4);
                        break;
                }

                frameCount = 0;
            }

            if (startClick)
            {
                exitSpeed = exitSpeed * 1.2f;
                logoPosition.X -= (int)exitSpeed;
                startPosition.X -= (int)exitSpeed;
                planeSize = planeSize <= 0 ? 0 : planeSize - 0.05f;

                if (logoPosition.X + logo.Width < -300 && startPosition.X + start.Width < -300 && planeSize <= 0)
                {
                    ExitScreen();
                    ScreenManager.AddScreen(new Select(), null);
                }
            }
            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            ScreenManager.SpriteBatch.Draw(start, startPosition, Color.White);
            ScreenManager.SpriteBatch.Draw(plane[planeIndex], planePosition, null, Color.White, 0, new Vector2(0, 0), planeSize, SpriteEffects.None, 0);
            ScreenManager.SpriteBatch.Draw(logo, logoPosition, Color.White);
            ScreenManager.SpriteBatch.End();
        }
    }
}
